<template>
  <div class="app-container">
    <el-card>
      <div slot="header">充值记录</div>
      <el-form :inline="true">
        <div class="filter-container">
          <el-input v-model="listQuery.keyword" placeholder="用户ID（用户名/手机号/邮箱）" style="width: 300px;" class="filter-item" @keyup.enter.native="handleFilter" />
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            搜索
          </el-button>
          <el-button class="filter-item" style="float: right" type="warning" icon="el-icon-download" @click="handleExport">
            导出Execl
          </el-button>
        </div>
      </el-form>
      <el-table ref="multipleTable" v-loading="loading" tooltip-effect="dark" :data="list" style="width: 100%;" stripe>
        <el-table-column align="left" type="selection" width="50" />
        <el-table-column align="left" label="ID" width="80">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="账号">
          <template slot-scope="scope">
            {{ scope.row.username }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="手机号">
          <template slot-scope="scope">
            {{ scope.row.phone }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="上级代理账号">
          <template slot-scope="scope">
            {{ scope.row.p_username }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="上级代理手机号">
          <template slot-scope="scope">
            {{ scope.row.p_phone }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付方式">
          <template slot-scope="scope">
            {{ scope.row.pay_type_str }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="账户类型">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="金额">
          <template slot-scope="scope">
            {{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付订单号">
          <template slot-scope="scope">
            {{ scope.row.pay_order_id }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否首次充值">
          <template slot-scope="scope">
            {{ scope.row.is_first }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="充值状态">
          <template slot-scope="scope">
            {{ scope.row.status_str }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="充值时间">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="10"
        :current-page="listQuery.page"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
  </div>
</template>

<script>
import { recharge } from '@/api/users'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import { parseTime } from '@/utils'

export default {
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      const res = await recharge(this.listQuery)
      this.list = res.data.data
      this.total = res.data.total
      this.listQuery.page = res.data.current_page
      this.loading = false
    },
    async handleExport() {
      this.loading = true
      const tHeader = ['账号', '手机号', '上级代理账号', '上级代理手机号', '支付方式', '账户类型', '金额', '支付订单号', '是否首次充值', '充值状态', '充值时间']
      const filterVal = ['username', 'phone', 'p_username', 'p_phone', 'pay_type_str', 'type', 'amount', 'pay_order_id', 'is_first', 'status_str', 'created_at']
      this.listQuery.export = 1
      const res = await recharge(this.listQuery)
      const list = res.data
      console.log(res.data)
      const data = this.formatJson(filterVal, list)
      export_json_to_excel({
        header: tHeader,
        data,
        filename: '充值记录',
        autoWidth: true,
        bookType: 'xlsx'
      })
      this.loading = false
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
